package cn.edu.zjgsu.ito.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import cn.edu.zjgsu.ito.service.UserPurchaseServiceI;
import cn.edu.zjgsu.ito.util.Constant;
import cn.edu.zjgsu.ito.vo.DemandIdVo;
import cn.edu.zjgsu.ito.vo.DeterminePriceSaveVo;
import cn.edu.zjgsu.ito.vo.FindCourierSaveVo;
import cn.edu.zjgsu.ito.vo.OrderIdVo;
import cn.edu.zjgsu.ito.vo.OrderNoCourierShowVo;
import cn.edu.zjgsu.ito.vo.OrderStateShowVo;
import cn.edu.zjgsu.ito.vo.PayCourierPriceSaveVo;
import cn.edu.zjgsu.ito.vo.PayCourierPriceShowVo;
import cn.edu.zjgsu.ito.vo.PurchaseCommentsSaveVo;
import cn.edu.zjgsu.ito.vo.PurchaseHomeShowVo;
import cn.edu.zjgsu.ito.vo.DemandMessageShowVo;
import cn.edu.zjgsu.ito.vo.RefundReasonSaveVo;
import cn.edu.zjgsu.ito.vo.UserHomeMessageShowVo;
import cn.edu.zjgsu.ito.vo.UserIdVo;

/**
 * 用户接单控制类
 * @author Administrator
 *
 */
@Controller
@RequestMapping("userPurchaseController")
public class UserPurchaseController {
	
	@Autowired
	private UserPurchaseServiceI userPurchaseService;

	/**
	 * 我来帮首页接口<br/>
	 * @return
	 */
	@RequestMapping(value="homePage",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> homePage(UserIdVo userIdVo,HttpSession session){
		int userId = -1;
		UserHomeMessageShowVo userHomeMessageShowVo = null;
		if(session.getAttribute("userId") != null){
			 userId = Integer.valueOf(session.getAttribute("userId").toString());
			 System.out.println( "==========" + userId);
			 userHomeMessageShowVo = userPurchaseService.userHome(userId);
		}
		
		Map<String,Object> result = new HashMap<String, Object>();
		
		List<PurchaseHomeShowVo> purchaseHomeShowVo = userPurchaseService.homePage();
		
		 
		result.put("user", userHomeMessageShowVo);
		result.put("msg",purchaseHomeShowVo);
		result.put("code", Constant.OK);
		
		return result;
		
	}
	
	/**
	 * 首页通过创建时间搜索
	 * @return
	 */
	@RequestMapping(value="orderByHomePage",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> orderByHomePage(int temp){
		
		Map<String,Object> result = new HashMap<String, Object>();
		
		List<PurchaseHomeShowVo> purchaseHomeShowVo = userPurchaseService.orderByHomePage(temp);
		
		result.put("msg",purchaseHomeShowVo);
		result.put("code", Constant.OK);
		
		return result;
		
	}
	
	/**
	 * 我来帮首页搜索
	 * @param temp
	 * @return
	 */
	@RequestMapping(value="seletByHomePage",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> seletByHomePage(String temp){
		
		Map<String,Object> result = new HashMap<String, Object>();
		
		List<PurchaseHomeShowVo> purchaseHomeShowVo = userPurchaseService.seletByHomePage(temp);
		
		result.put("msg",purchaseHomeShowVo);
		result.put("code", Constant.OK);
		
		return result;
		
	}
	
	
	/**
	 * 查看订单详情接口<br/>
	 * 根据求购id，查看求购单详细信息
	 * @param demandIdVo
	 * @return
	 */
	@RequestMapping("demandMessage")
	@ResponseBody
	public Map<String,Object> demandMessage(DemandIdVo demandIdVo){
		
		Map<String, Object> result = new HashMap<String, Object>();
		
		DemandMessageShowVo demandMessageShowVo = userPurchaseService.demandMessage(demandIdVo);
		
		result.put("msg", demandMessageShowVo);
		result.put("code", Constant.OK);
		
		return result;
	}
	
	/**
	 * 用户确认接单接口<br/>
	 * 
	 * @return
	 */
	@RequestMapping(value="acceptSuccess",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> acceptSuccess(DemandIdVo demandIdVo,HttpSession session){
		
		int userId = Integer.valueOf(session.getAttribute("userId").toString());
		
		Map<String, Object> result = new HashMap<String, Object>();
		
		OrderIdVo orderIdVo = userPurchaseService.acceptSuccess(demandIdVo,userId);
		
		result.put("msg", orderIdVo);
		result.put("code", Constant.OK);
		
		return result;
	}
	
	/**
	 * 接单成功后页面接口
	 */
	@RequestMapping(value="orderMessage",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> orderMessage(OrderIdVo orderIdVo){
		
		Map<String, Object> result = new HashMap<String, Object>();
		
		OrderNoCourierShowVo orderNoCourierShowVo = userPurchaseService.orderMessage(orderIdVo);
		
		result.put("msg", orderNoCourierShowVo);
		result.put("code", Constant.OK);
		
		return result;
	}
	
	/**
	 * 用户矫正金额接口
	 * @param determinePriceSaveVo
	 * @return
	 */
	@RequestMapping(value="determinePrice",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> determinePrice(DeterminePriceSaveVo determinePriceSaveVo){
		
		Map<String, Object> result = new HashMap<String, Object>();
		
		OrderStateShowVo orderStateShowVo = userPurchaseService.determinePrice(determinePriceSaveVo);
		
		result.put("msg", orderStateShowVo);
		result.put("code", Constant.OK);
		
		return result;
	}
	
	/**
	 * 正在购买接口
	 * @return
	 *//*
	@RequestMapping("isBuying")
	@ResponseBody
	public Map<String,Object> isBuying(OrderIdVo orderIdVo){
		
		Map<String, Object> result = new HashMap<String, Object>();
		
		OrderStateShowVo orderStateShowVo = userPurchaseService.isBuying(orderIdVo);
		
		result.put("msg",orderStateShowVo);
		result.put("code", Constant.OK);
		
		return result;
	}*/
	
	/**
	 * 购买完成
	 * @param orderIdVo
	 * @return
	 *//*
	@RequestMapping("buyComplete")
	@ResponseBody
	public Map<String, Object> buyComplete(OrderIdVo orderIdVo){
		
		Map<String, Object> result = new HashMap<String, Object>();
		
		OrderStateShowVo orderStateShowVo = userPurchaseService.buyComplete(orderIdVo);
		
		result.put("msg",orderStateShowVo);
		result.put("code", Constant.OK);
		
		return result;
		
	}*/
	
	/**
	 * 发布消息，寻找快递员
	 * @param findCourierSaveVo
	 * @return
	 */
	@RequestMapping(value="findCourier",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findCourier(FindCourierSaveVo findCourierSaveVo){
		
		Map<String, Object> result = new HashMap<String, Object>();
		
		userPurchaseService.findCourier(findCourierSaveVo);
		
		result.put("code", Constant.OK);
		
		return result;
	}
	
	
	/**
	 * 支付快递费，选择代金券接口
	 * @param payCourierPriceSaveVo
	 * @return
	 */
	@RequestMapping(value="payCourierPricePageVoucher",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> payCourierPricePageVoucher(PayCourierPriceSaveVo payCourierPriceSaveVo){
	Map<String, Object> result = new HashMap<String, Object>();
		
		OrderIdVo orderIdVo = userPurchaseService.payCourierPricePageVoucher(payCourierPriceSaveVo);
		
		result.put("msg", orderIdVo);
		result.put("code",Constant.OK);
		return result;
	}
	
	/**
	 * 支付快递费页面接口
	 * @param orderIdVo
	 * @return
	 */
	@RequestMapping(value="payCourierPricePage",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> payCourierPricePage(OrderIdVo orderIdVo){

		Map<String, Object> result = new HashMap<String, Object>();

		PayCourierPriceShowVo payCourierPriceShowVo = userPurchaseService.payCourierPricePage(orderIdVo);

		result.put("msg", payCourierPriceShowVo);

		result.put("code", Constant.OK);

		return result;

	}
	
	/**
	 * 支付快递费接口
	 * @param orderIdVo
	 * @return
	 */
	@RequestMapping(value="payCourierPrice",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> payCourierPrice(OrderIdVo orderIdVo){
		Map<String, Object> result = new HashMap<String, Object>();
		userPurchaseService.payCourierPrice(orderIdVo);
		result.put("code", Constant.OK);
		return result;
	}
	
	/**
	 * 用户处理退款
	 * @param findCourierSaveVo
	 * @return
	 */
	@RequestMapping(value="acceptRefund",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> acceptRefund(RefundReasonSaveVo refundReasonSaveVo){
		
		Map<String, Object> result = new HashMap<String, Object>();
		
		OrderStateShowVo orderStateShowVo = userPurchaseService.acceptRefund(refundReasonSaveVo);
		
		result.put("msg",orderStateShowVo);
		result.put("code",Constant.OK);
		
		return result;
	}
	
	
	/**
	 * 代购者评价
	 * @param purchaseCommentsSaveVo
	 * @return
	 */
	@RequestMapping(value="userPurchaseComments",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> userPurchaseComments(PurchaseCommentsSaveVo purchaseCommentsSaveVo){

		Map<String,Object> result = new HashMap<String ,Object>();

		userPurchaseService.userPurchaseComments(purchaseCommentsSaveVo);

		result.put("code", Constant.OK);

		return result;

	}
	
	
}
